
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-10-20
 * Time: 15:31
 */
class TreeNode {
    public int data;
    public TreeNode lchild;
    public TreeNode rchild;
    public TreeNode(int data) {
        this.data = data;
    }
}
public class H1020 {
    public int postIndex;
    private int findIndex(int[] inOrder, int inbegin, int inend, int val) {
        for (int i = inbegin; i <= inend; i++) {
            if(inOrder[i] == val) {
                return i;
            }
        }
        return -1;
    }
    private TreeNode buildTreeChild(int[] postOrder, int[] inOrder, int inbegin, int inend) {
        if(inbegin > inend) {
            return null;
        }
        TreeNode treeNode = new TreeNode(postOrder[postIndex]);
        int inIndex = findIndex(inOrder, inbegin, inend, postOrder[postIndex]);
        postIndex--;
        treeNode.rchild = buildTreeChild(postOrder, inOrder, inIndex + 1, inend);
        treeNode.lchild = buildTreeChild(postOrder, inOrder, inbegin, inIndex - 1);
        return treeNode;
    }
    private void inOrderPrint(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(" " + root.data);
        inOrderPrint(root.lchild);
        inOrderPrint(root.rchild);
    }
    public void TreeNodeBuild() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] postOrder = new int[n];
        for (int i = 0; i < n; i++) {
            postOrder[i] = scanner.nextInt();
        }
        int[] inOrder = new int[n];
        for (int i = 0; i < n; i++) {
            inOrder[i] = scanner.nextInt();
        }
        postIndex = postOrder.length - 1;
        TreeNode root = buildTreeChild(postOrder, inOrder, 0, postOrder.length - 1);
        System.out.print("Preorder: ");
        inOrderPrint(root);
    }

    public static void main(String[] args) {
        H1020 h1020 = new H1020();

        h1020.TreeNodeBuild();
    }
}
